package com.fanfanwang.ffw.app.ui.login;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.MainActivity;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.ViewPagerAdapter;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.common.LogUtil;
/**引导页*/
public class GuideAct extends BaseActivity implements OnClickListener, OnPageChangeListener{
	
	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;
	private Button btnRegister;
	private Button btnLogin;
	private Button btnTiyan;
	private Button btnTiaoguo;
	//引导图片资源
	private static final int[] pics = { R.drawable.guide_pic1,
			R.drawable.guide_pic2, R.drawable.guide_pic3,
			R.drawable.guide_pic4};
	
	//底部小店图片
	private ImageView[] dots ;
	
	//记录当前选中位置
	private int currentIndex;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE); // 取消标题
        setContentView(R.layout.act_guide);
        initWidget();
        AppManager.getAppManager().addActivity(this);
        views = new ArrayList<View>();
       
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        		LinearLayout.LayoutParams.MATCH_PARENT);
        
        //初始化引导图片列表
        for(int i=0; i<pics.length; i++) {
        	ImageView iv = new ImageView(this);
        	iv.setLayoutParams(mParams);
        	iv.setImageResource(pics[i]);
        	views.add(iv);
        }
        vp = (ViewPager) findViewById(R.id.guide_view);
        //初始化Adapter
        vpAdapter = new ViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        //绑定回调
        vp.setOnPageChangeListener(this);
        
        //初始化底部小点
        initDots();
        
    }
    
    private void initDots() {/*
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

		dots = new ImageView[pics.length];
		
		//循环取得小点图片
		for (int i = 0; i < pics.length; i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);//都设为灰色
			dots[i].setOnClickListener(this);
			dots[i].setTag(i);//设置位置tag，方便取出与当前位置对应
		}
		
		currentIndex = 0;
		dots[currentIndex].setEnabled(false);//设置为白色，即选中状态
    */}
    
    /**
     *设置当前的引导页 
     */
    private void setCurView(int position)
    {
		if (position < 0 || position >= pics.length) {
			return;
		}

		vp.setCurrentItem(position);
    }

    /**
     *设置当前引导小点的选中 
     */
    private void setCurDot(int positon)
    {/*
    	Log.e("setCurDot", "  positon  >>  " + positon);
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}
//		dots[positon].setEnabled(false);
		dots[positon % views.size()].setEnabled(true);

		currentIndex = positon % views.size();
    */}

    //当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	//当当前页面被滑动时调用
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	//当新的页面被选中时调用
	@Override
	public void onPageSelected(int arg0) {
		//设置底部小点选中状态
		setCurDot(arg0);
		if(arg0 == pics.length-1){
			btnTiaoguo.setVisibility(View.GONE);
			btnTiyan.setVisibility(View.VISIBLE);
		}else {
			btnTiaoguo.setVisibility(View.VISIBLE);
			btnTiyan.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		int position = (Integer)v.getTag();
		setCurView(position);
		setCurDot(position);
	}

	public void initWidget() {
		// TODO Auto-generated method stub
		btnRegister = (Button) findViewById(R.id.gu_btn_register);
		btnLogin = (Button) findViewById(R.id.gu_btn_login);
		btnTiyan = (Button) findViewById(R.id.gu_btn_tiyan);
		btnTiaoguo = (Button) findViewById(R.id.gu_btn_tiaoguo);
		btnTiaoguo.setVisibility(View.VISIBLE);
		btnTiyan.setVisibility(View.GONE);
//		LogUtil.e(GuideAct.class+"", "initWidget>>>>>"+btnRegister);
		widgetClick();
	}

	public void widgetClick() {
		// TODO Auto-generated method stub
//		btnRegister.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
////				LogUtil.e(GuideAct.class+"", "btnRegister   click");
//				startActivity(new Intent(GuideAct.this, HomeActivity.class));
//				finish();
//			}
//		});
//		btnLogin.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Intent intent;
//				// TODO Auto-generated method stub
//				intent = new Intent(GuideAct.this, LoginAct.class);
//				startActivity(intent);
////				finish();
//			}
//		});
//		btnTiaoguo.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Intent intent;
//				// TODO Auto-generated method stub
//				intent = new Intent(GuideAct.this, HomeActivity.class);
//				startActivity(intent);
//				finish();
//			}
//		});	
		btnTiyan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent;
				// TODO Auto-generated method stub
				intent = new Intent(GuideAct.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		});	
	}


	@Override
	public void initData() {
		// TODO Auto-generated method stub
		
	}
	
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		 
//        if (keyCode == KeyEvent.KEYCODE_BACK
//                 && event.getRepeatCount() == 0) {
//            //do something...
////        	showCheckDialog(getString(R.string.guide_exit));
//             return true;
//         }
//         return super.onKeyDown(keyCode, event);
//     }

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		
	}
}